﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;

namespace GsifWeb4.Helpers
{
    public static class CalenderExtensions
    {
        public static MvcHtmlString Calender(this HtmlHelper helper, bool showAdmin)
        {
            //return String.Format("<label for='{0}'>{1}</label>", target, text);
            StringBuilder javascript = new StringBuilder();
            javascript.AppendLine("<script type=\"text/javascript\">");
            javascript.AppendLine("function DateSelected(dateText) {");
            javascript.AppendLine("$(\"#calenderInfo\").html('<img src=\"/Images/SiteImages/ajax-loader.gif\" />');");
            javascript.AppendLine("var selectedDate = new Date(dateText);");
            javascript.AppendLine("var myUrl = '/Calender/AjaxGetEventsOnDate';");
            javascript.AppendLine("var year = selectedDate.getFullYear();");
            javascript.AppendLine("var month = selectedDate.getMonth() + 1;");
            javascript.AppendLine("var day = selectedDate.getDate();");
            javascript.AppendLine("$.post(myUrl,");
            javascript.AppendLine("{");
            javascript.AppendLine("year: year,");
            javascript.AppendLine("month: month,");
            javascript.AppendLine("day: day");
            javascript.AppendLine("},");
            javascript.AppendLine("function (calenderEvents) {");
            javascript.AppendLine("// Ta bort animation");
            javascript.AppendLine("$(\"#calenderInfo\").html('');");

            javascript.AppendLine("$('#calenderInfo').append('<b>Aktiviteter: ' + DateToYMD(year, month, day) + '</b>');");
            javascript.AppendLine("$('#calenderInfo').append('<br/><br/>');");
            javascript.AppendLine("// Lägg till texten för varje event");
            javascript.AppendLine("$.each(calenderEvents, function (i, calenderEvent) {");
            javascript.AppendLine("$('#calenderInfo').append(calenderEvent.Text)");
            javascript.AppendLine("");

            // Lägg in villkor beroende på plats och inloggad
            if (showAdmin && HttpContext.Current.User.Identity.IsAuthenticated)
            {
                javascript.AppendLine(".append('<br><a href= \"/Calender/Delete/' + calenderEvent.Id + '/' + calenderEvent.TypeOfEvent + '\"</a>' + 'Ta bort')");
                javascript.AppendLine(".append('<br><a href= \"/Calender/Edit/' + calenderEvent.Id + '/' + calenderEvent.TypeOfEvent + '\"</a>' + ' Ändra')");
                //javascript.AppendLine(".append('<br><a href= \"/Calender/Edit?Id=' + calenderEvent.Id + '?calenderEventType=' + calenderEvent.TypeOfEvent + '\"</a>' + ' Ändra')");            
                //helper.ActionLink("Lägg till träning", "Edit", "Calender",  new { id =  (int)GsifWeb4.CalenderHelper.CalenderEvent.EventType.Training }, null) <br />
            }
            javascript.AppendLine(".append('<br><br>');");
            javascript.AppendLine("});");
            javascript.AppendLine("});");
            javascript.AppendLine("}");

            javascript.AppendLine("</script>");

            return MvcHtmlString.Create(javascript.ToString());
        }


        public static MvcHtmlString Alert(this HtmlHelper helper, string text)
        {
            StringBuilder javascript = new StringBuilder();
            javascript.AppendLine("<script type=\"text/javascript\">");
            javascript.AppendLine("alert('" + text + "'");
            javascript.AppendLine("</script>");
            return MvcHtmlString.Create(javascript.ToString());
        }
    }
}